function getCrudUsers(renderTo,url) {
    var ret = defaultCRUD(renderTo,url,{noinit:true});

    ret.readRenderer = function(type,data) {
        if (type == "attribute" && data == "password") {
            return null;
        } else if (type == "afterAttributes") {
            var ret = CRUDrenderer.prototype.readRenderer.call(this,type,data);
            ret = ret.add( $("<th>password</th>") );
            return ret;
        } else if (type == "item" && data[0] == "password") {
            return null;
        } else if (type == "afterItems") {
            var self = this;
            var ret = CRUDrenderer.prototype.readRenderer.call(this,type,data);
            var edit = $('<a href="">set</a>');
            edit.click( function() {
                self._update_password_only = true;
                self.renderUpdate(data);
                delete self._update_password_only;
                return false;
            });
            ret = ret.add( $("<td/>").append(edit) );
            return ret;
        } else {
            return CRUDrenderer.prototype.readRenderer.call(this,type,data);
        }
    };

    ret.updateRenderer = function(type,data) {
        if (this._update_password_only) {
            if (type == "item") {
                if (data[0] == "password" || data[0] == "id") {
                    return CRUDrenderer.prototype.updateRenderer.call(this,type,data);
                } else {
                    return null;
                }
            } else {
                return CRUDrenderer.prototype.updateRenderer.call(this,type,data);
            }
        } else {
            if (type == "item" && data[0] == "password" && this._update_id != null) {
                return null;
            } else {
                return CRUDrenderer.prototype.updateRenderer.call(this,type,data);
            }
        }
    };

    return ret;
}

function getCrudBooks(renderTo,url) {
    var ret = defaultCRUD(renderTo,url,{noinit:true});
    
    ret._my_rnd = Math.random().toString().slice(2);

    ret.readRenderer = function(type,data) {
        if (type == "item" && data[0] == "category") {
            if (data[1] && data[1].name) {
                return $('<td>'+data[1].name+'</td>');
            } else {
                return $('<td></td>');
            }
        } else if (type =="item" && data[0] == "category_id") {
            return null;
        } else if (type =="attribute" && data == "category_id") {
            return null;
        }  else {
            return CRUDrenderer.prototype.readRenderer.call(this,type,data);
        }
    };

    ret.updateRenderer = function(type,data) {
        if (type == "item" && (data[0] == "created_at" 
            || data[0] == "updated_at" || data[0] == "no_of_reviews") ) {
            return null;
        } else if (type=="item" && data[0] == "isbn") {
            var ret = CRUDrenderer.prototype.updateRenderer.call(this,type,data);
            var input = ret.find("input");
            var self = this;
            input.focusout( function() {
                var baseUrl = 'http://xisbn.worldcat.org/webservices/xid/isbn/';
                var params = {
                    method:'getMetadata',
                    fl: 'author,title',
                    format: 'json'
                };
                var isbn = $(this).val();
                $.getJSON(baseUrl+isbn+'?callback=?',params,function(data) {
                    if (data && data.stat == "ok") {
                        var desc = $(self.renderTo+' input[name="description"]');
                        var title = $(self.renderTo+ ' input[name="title"]');
                        desc.val(data.list[0].author);
                        title.val(data.list[0].title);
                    }
                });
            });
            return ret;
        } else if (type =="item" && data[0] == "category") {
            if (this.categories) {
                var select = $('<select name="category_id"></select>');
                select.append( $('<option value="0">none</option>') );
                for (var i in this.categories) {
                    var option = $('<option></option>', {
                       text: this.categories[i].name,
                       value:this.categories[i].id
                    });
                    if (data[1] && data[1].id
                         && data[1].id == this.categories[i].id ) {
                        option.attr( { selected: 'selected' } );
                    }
                    select.append(option);
                }
                var ret = $("<tr></tr>");
                ret.append("<td>category </td>");
                ret.append( $("<td></td>").append(select) );
                return ret;
            } else {
                if (data[1] && data[1].id) {
                    return CRUDrenderer.prototype.updateRenderer.call(
                        this,type,[data[0],data[1].id]);
                } else {
                    return CRUDrenderer.prototype.updateRenderer.call(this,type,data);
                }
            }
        } else {
            return CRUDrenderer.prototype.updateRenderer.call(this,type,data);
        }
    };

    ret.setCategories = function(cats) {
        this.categories = cats;
    };

    return ret;
}

function getCrudReviews(renderTo,url) {
    var ret = defaultCRUD(renderTo,url,
                    {noinit:true,create:false,update:false});

    var rnd = Math.random().toString().slice(2);
    $(renderTo).prepend('<div id="refreshHeader'+rnd+'"></div>');

    ret.setHeader('#refreshHeader'+rnd,{
        browse:function(crud) {
            crud.refresh(function() { crud.renderRead(); } );
        }
    });

    ret.readRenderer = function(type,data) {
        if (type == "item" && data[0] == "book") {
            return $("<td>"+data[1].title+"</td>");
        } else if (type =="item" && data[0] == "user") {
            return $("<td>"+data[1].name+"</td>");
        }else {
            return CRUDrenderer.prototype.readRenderer.call(this,type,data);
        }
    };

    return ret;
}

function getCrudOrders(renderTo) {
    var ret = defaultCRUD(renderTo,"controllers/orders.php",{noinit:true,update:false,create:false});
    
    var rnd = Math.random().toString().slice(2);
    
    $(renderTo).prepend('<div id="refreshHeader'+rnd+'"></div>');
    ret.setHeader('#refreshHeader'+rnd,{
        browse:function(crud) {
            crud.refresh(function() { crud.renderRead(); } );
        }
    });

    ret.recordRenderer = function(type,data) {
        if (type=="recordWrapper") {
            return null;
        } else if (type=="record") {
      
        var orderInfo= $(
          '<tr class="orderInfo" style="display:none">'+
          '<td colspan="8"></td>'+
          '</tr>'
        );

        var a_viewItems = $('<a href="">view</a>');
        a_viewItems.click(function() {
            console.log(orderInfo);
            orderInfo.toggle();
            return false;
        });
        var a_delete = $('<a href="">delete</a>');
        a_delete.click(function() {
            $.post("controllers/orders.php",{remove:1,id:data.id},
                function(data) { 
                    if (data && data.error) {
                        alert(data.error);
                    } else {
                        ret.refreshRead();
                    }
            },'json');
            return false;
        });
        var a_dispatched = $('<a href="">dispatched</a>');
        a_dispatched.click(function() {
            $.post("controllers/orders.php",{mark_dispatched:1,id:data.id},
                function(data) { 
                    if (data && data.error) {
                        alert(data.error);
                    } else {
                        ret.refreshRead();
                    }
            },'json');
            return false;
        });

        var record = $( 
            '<tr>'+
            '<td>'+data.id+'</td>'+
            '<td>'+data.user.name+'</td>'+
            '<td>'+data.contact+'</td>'+
            '<td>'+data.user.email+'</td>'+
            '<td>'+data.address+'</td>'+
            '<td>'+data.creditcard+'</td>'+
            '<td>'+data.note+'</td>'+
            '<td>'+data.created_at+'</td>'+
            '<td>'+data.price+'</td>'+
            '</tr>'
        );
        if (data.dispatched == 1) {
            record.addClass("orderDispatched");
        }

        record.append( $('<td></td>').append(a_viewItems) );
        record.append( $('<td></td>').append(a_delete) );
        if (data.dispatched != 1) {
            record.append( $('<td></td>').append(a_dispatched) );
        }

        record = record.add( orderInfo ); 

        $(data.items).each(function(i,data) {
            if (data.item.id == 0) {
                itemInfo = $("<p>BOOK DELETED!</p>");
            } else {
                itemInfo = $(
                 '<ul>'+
                 '<li>id: '+data.item.id+'</li>'+
                 '<li>isbn: '+data.item.isbn+'</li>'+
                 '<li>price: '+data.item.price+'</li>'+
                 '<li>title: '+data.item.title+'</li>'+
                 '<li>available on stock: '+data.item.on_stock+'</li>'+
                 '<li>QTY: '+data.quantity+'</li>'+
                 '</ul>'
                );
           }
        
           orderInfo.children().first().append( $("<li>item "+(i+1)+": </li>").append(itemInfo));
       });
       

       return record;
       }
       return null;
    }

    ret.readRenderer = function(type,data) {
        if (type=="wrapper") {
            return $("<table></table>");
        } else if (type=="attributesWrapper") {
            return $("<tr></tr>");
        } else if (type=="beforeAttributes") {
            return $(
            '<th>id</th>'+
            '<th>by</th>'+
            '<th>contact</th>'+
            '<th>email</th>'+
            '<th>address</th>'+
            '<th>credit card no.</th>'+
            '<th>note</th>'+
            '<th>created_at</th>'+
            '<th>price</th>'+
            '<th>items</th>'+
            '<th>delete</th>'+
            '<th>mark dispatched</th>'
            );
        }
        return null;
    };

    return ret;
}
